using System;
using System.Data;
using System.Collections.Generic;
using System.Web.Caching;
using System.Web.UI;

namespace NimblePros.SimpleCms
{
    public class CmsPage
    {
        #region Constructors

        public CmsPage() : this(new DataHelper(CmsSettings.StaticConnectionString))
        {
        }

        private CmsPage(DataHelper dataHelper)
        {
            _dataHelper = dataHelper;
        }

        public CmsPage(DataRow pageData)
            : this(new DataHelper(CmsSettings.StaticConnectionString))
        {
            SetPageData(pageData);
        }


        public CmsPage(string url)
            : this(new DataHelper(CmsSettings.StaticConnectionString))
        {
            DataRow pageData = _dataHelper.SelectPage(url);
            if (pageData != null)
            {
                SetPageData(pageData);
            }
        }

        public CmsPage(int id)
            : this(new DataHelper(CmsSettings.StaticConnectionString))
        {
            DataRow pageData = _dataHelper.SelectPage(id);
            if (pageData != null)
            {
                SetPageData(pageData);
            }
        }

        private void SetPageData(DataRow pageData)
        {
            Id = int.Parse(pageData["Id"].ToString());
            Url = pageData["Url"].ToString();
            Title = pageData["Title"].ToString();
            Description = pageData["Description"].ToString();
            Keywords = pageData["Keywords"].ToString();
            ContentText = pageData["ContentText"].ToString();
            MasterPage = pageData["MasterPage"].ToString();
            Enabled = (bool)pageData["Enabled"];
        }


        #endregion

        #region Properties

        private readonly DataHelper _dataHelper;

        public int Id { get; set; }

        public string Url { get; set; }

        public string Title { get; set; }

        public string Description { get; set; }

        public string Keywords { get; set; }

        public string ContentText { get; set; }

        public string MasterPage { get; set; }

        public bool Enabled { get; set; }

        #endregion

        /// <summary>
        /// Selects a page by its unique Url and accesses the cache if able. 
        /// If not it adds the page it retrieves to the cache.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cache"></param>
        /// <returns></returns>
        public static CmsPage SelectByUrl(string url, Cache cache)
        {
            string cacheKey = url;
            var cacheItem = cache[url] as CmsPage;
            if (cacheItem == null)
            {
                cacheItem = new CmsPage(url);
                cache.Insert(cacheKey, cacheItem);
            }

            return cacheItem;
        }

        public static List<CmsPage> SelectAll()
        {
            var dataHelper = new DataHelper(CmsSettings.StaticConnectionString);
            DataTable allPageData = dataHelper.SelectAllPages();

            List<CmsPage> allPages = null;

            if (allPageData != null)
            {
                allPages = new List<CmsPage>(allPageData.Rows.Count);

                foreach (DataRow pageData in allPageData.Rows)
                {
                    allPages.Add(new CmsPage(pageData));
                }
            }

            return allPages;
        }

        public void Save(Page myPage)
        {
            if (this.Id > 0)
            {
                _dataHelper.UpdatePage(this);

                if (myPage.Cache[this.Url] != null)
                {
                    myPage.Cache.Remove(this.Url);
                }
            }
            else
            {
                _dataHelper.InsertPage(this);
            }
        }

        public static void ToggleStatus(string url, Page myPage)
        {
            var dataHelper = new DataHelper(CmsSettings.StaticConnectionString);
            dataHelper.TogglePageStatus(url);
            if (myPage.Cache[url] != null)
            {
                myPage.Cache.Remove(url);
            }
        }
    }
}
